package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.LogicException;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.JdbcProperties;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@Service
//@Transactional mongodb的事务必须是复制集的方式
public class RegionServiceImpl implements IRegionService{
    @Autowired
    private RegionRepository repository;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(Region region) {
        region.setId(null);//把id设置为null
        repository.save(region);//存入region对象

    }

    @Override
    public void update(Region region) {
        repository.save(region);//编辑和保存是一样的,更新之后则要保存region对象

    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }

    @Override
    public Region get(String id) {
        //orEls,查询id,存在就返回,不承诺在就返回null
        return repository.findById(id).orElse(null);

    }

    @Override
    public List<Region> list() {
        //查询所有
        return repository.findAll();
    }

    @Override
    public Page<Region> query(RegionQuery qo) {

        //分页查询
        //查询对象
        Query query = new Query();
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,Region.class,query,pageable);

//        Long totalCount = template.count(query,Region.class);
//        if (totalCount == 0){
//            return Page.empty();//空的分页对象
//        }
//        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1,
//                qo.getPageSize(), Sort.Direction.ASC, "_id");
//
//        query.with(pageable);
//        List<Region> list = template.find(query,Region.class);
//        return new PageImpl<>(list,pageable,totalCount);


    }

    @Override
    public void saveOrUpdate(Region region) {

        if (StringUtils.hasLength(region.getId())){
            //update
            //特殊案例,页面更新的数据,刚好跟集合中字段个数一致
            //所以这种等价于全量的更新
            this.update(region);
        }else {
            //save
            this.save(region);
        }

    }

    @Override
    public void changeHotValue(int hot, String id) {
        //1.查询
        Region region = this.get(id);
        //2.替换
        region.setIshot(hot);
        //3.更新
        this.update(region);
    }

    @Override
    public List<Region> queryHotRegion() {
        //1.状态是热门的
        //2.通过序号排序
        return repository.findByIshotOrderBySequence(Region.STATE_HOT);
    }


}
